#include "FightingCapacityCacheData.h"


FightingCapacityCacheData* FightingCapacityCacheData::mInstance = NULL;

void FightingCapacityCacheData::CreateInstance()
{
	if (mInstance == nullptr)
	{
		mInstance = new FightingCapacityCacheData();
	}
}

FightingCapacityCacheData* FightingCapacityCacheData::GetInstance()
{
	return mInstance;
}

FightingCapacityCacheData::FightingCapacityCacheData()
	: m_maxCount(0)
{
}

FightingCapacityCacheData::~FightingCapacityCacheData()
{
	Clear();
}

bool FightingCapacityCacheData::Reload()
{
	_safe_guard_begin;

	Clear();

	Initialization();

	_safe_guard_end;

	return false;
}

void FightingCapacityCacheData::Clear()
{
	_safe_guard_begin;

	auto itor = m_mapCacheData.begin();
	for (; itor != m_mapCacheData.end(); ++itor)
	{
		_safe_delete(itor->second);
	}

	m_mapCacheData.clear();

	_safe_guard_end;
}

bool FightingCapacityCacheData::Initialization()
{
	_safe_guard_begin;

	Json::Value _jv = ReadJsonFile("./table/fightingCapacity.json");
	if (_jv == Json::nullValue)
	{
		LOG_ERROR("default","ReadJsonFile -> table/fightingCapacity.json failed ......");
		return false;
	}

	m_maxCount = _jv.size();

	Json::Value::Members _tmp = _jv.getMemberNames();
	uint32_t _size = (uint32_t)(_tmp.size());
	for (uint32_t i = 0; i < _size; ++i)
	{
		uint32_t _idx = (uint32_t)atoi(_tmp[i].c_str());

		AddData(_idx, _jv[_tmp[i]]);
	}

	return true;

	_safe_guard_end;

	return false;
}

void FightingCapacityCacheData::AddData(uint32_t id, Json::Value& jm)
{
	_safe_guard_begin;

	auto itor = m_mapCacheData.find(id);
	if (itor == m_mapCacheData.end())
	{
		FightingCapacity* p = new FightingCapacity;
		_safe_check(p);

		p->mId = jm["star"].asUInt();
		p->mHp = jm["hp"].asDouble();
		p->mPhysicalAttack = jm["physical_attack"].asDouble();
		p->mMagicAttack = jm["magic_attack"].asDouble();
		p->mArmor = jm["armor"].asDouble();
		p->mMagicResist = jm["magic_resist"].asDouble();
		p->mPhysicalCritical = jm["physical_critical"].asDouble();
		p->mDodge = jm["dodge"].asDouble();
		p->mHitRatio = jm["hit_ratio"].asDouble();
		p->mArmorPenetration = jm["armor_penetration"].asDouble();
		p->mMagicPenetration = jm["magic_penetration"].asDouble();
		p->mSuckBlood = jm["suck_blood"].asDouble();
		p->mSpellVamp = jm["spell_vamp"].asDouble();
		p->mTenacity = jm["tenacity"].asDouble();
		p->mSkill = jm["skill"].asDouble();

		m_mapCacheData[id] = p;
	}

	_safe_guard_end;
}

FightingCapacity* FightingCapacityCacheData::GetData(uint32_t id)
{
	_safe_guard_begin;

	auto itor = m_mapCacheData.find(id);
	if (itor != m_mapCacheData.end())
	{
		return itor->second;
	}
	_safe_guard_end;

	return nullptr;
}

bool FightingCapacityCacheData::GetDataJsonString(uint32_t id, Json::Value& _jm)
{
	_safe_guard_begin;

	FightingCapacity* p = GetData(id);
	if (nullptr == p)
	{
		LOG_ERROR("default","Error4");
		return false;
	}

	_jm.clear();

	_jm["id"] = Json::Value(p->mId);
	_jm["hp"] = Json::Value(p->mHp);
	_jm["physical_attack"] = Json::Value(p->mPhysicalAttack);
	_jm["magic_attack"] = Json::Value(p->mMagicAttack);
	_jm["armor"] = Json::Value(p->mArmor);
	_jm["magic_resist"] = Json::Value(p->mMagicResist);
	_jm["physical_critical"] = Json::Value(p->mPhysicalCritical);
	_jm["dodge"] = Json::Value(p->mDodge);
	_jm["hit_ratio"] = Json::Value(p->mHitRatio);
	_jm["armor_penetration"] = Json::Value(p->mArmorPenetration);
	_jm["magic_penetration"] = Json::Value(p->mMagicPenetration);
	_jm["suck_blood"] = Json::Value(p->mSuckBlood);
	_jm["spell_vamp"] = Json::Value(p->mSpellVamp);
	_jm["tenacity"] = Json::Value(p->mTenacity);
	_jm["skill"] = Json::Value(p->mSkill);

	_safe_guard_end;

	return true;
}

uint32_t FightingCapacityCacheData::GetDataCount()
{
	_safe_guard_begin;

	return m_maxCount;

	_safe_guard_end;
}

